package com.ruiwen.imuser.controller;

import com.ruiwen.im.common.until.UserIdUtil;
import com.ruiwen.imuser.model.entity.UserEntity;
import com.ruiwen.imuser.service.UserService;
import com.ruiwen.im.common.response.IMCommonResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@Api(tags = "用户相关信息")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
//    @Autowired
//    private BCryptPasswordEncoder passwordEncoder;

    @ApiOperation("增加用户")
    @PostMapping(value = "/addUser"/**,produces = "application/json;charset=UTF-8"*/)
    public IMCommonResponse<String> addUser(@ApiParam(name = "password", value = "密码") @RequestParam(value = "password", required = true) String password,
                                            @ApiParam(name = "username", value = "用户名") @RequestParam(value = "username", required = true) String username,
                                            @ApiParam(name = "age", value = "年龄") @RequestParam(value = "age", required = false) Integer age,
                                            @ApiParam(name = "address", value = "地址") @RequestParam(value = "address", required = false) String address,
                                            @ApiParam(name = "phone", value = "手机号码") @RequestParam(value = "phone", required = true) Integer phone,
                                            @ApiParam(name = "gender", value = "性别") @RequestParam(value = "gender", required = true) Character gender,
                                            @ApiParam(name = "birth", value = "出生日期") @RequestParam(value = "birth", required = false) String birth
    ) {
//        List<String> columnsList = new LinkedList<>();
//        List<Object> valuesList = new LinkedList<>();
//        columnsList.add("password");
//        valuesList.add(passwordEncoder.encode(password));
//        columnsList.add("username");
//        valuesList.add(username);
//        columnsList.add("phone");
//        valuesList.add(phone);
//        columnsList.add("create_time");
//        valuesList.add(LocalDateTime.now());
//        columnsList.add("gender");
//        valuesList.add(gender);
//        if (!StringUtils.isEmpty(address)) {
//            columnsList.add("address");
//            valuesList.add(address);
//        }
//        if (!StringUtils.isEmpty(birth)) {
//            columnsList.add("birth");
//            valuesList.add(LocalDate.parse(birth, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
//        }
        UserEntity userEntity = new UserEntity();
        userEntity.setGender(gender);
//        userEntity.setPassword(passwordEncoder.encode(password));
        userEntity.setPassword(password);
        userEntity.setUserId(UserIdUtil.getUUIDInUserId());
        userEntity.setCreateTime(LocalDateTime.now());
        userEntity.setPhoneNumber(phone);
        userEntity.setUserName(username);
        if (!StringUtils.isEmpty(address)) {
            userEntity.setAddress(address);
        }
        if (!StringUtils.isEmpty(age)) {
            userEntity.setAge(age);
        }
        if (!StringUtils.isEmpty(birth)) {
            userEntity.setBirth(LocalDateTime.parse(birth, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        return userService.addUser(userEntity);

    }

    @ApiOperation("查询用户")
    @GetMapping(value = "/getById")
    public IMCommonResponse<UserEntity> getById(@ApiParam(name = "id", value = "参数id") @RequestParam(value = "id", required = true) Integer id) {
        return userService.getById(id);
    }

    @ApiOperation("更新")
    @PostMapping(value = "/updateById")
    public IMCommonResponse<Object> updateById(@ApiParam(name = "id", value = "参数id") @RequestParam(value = "id", required = true) Integer id,
                                               @ApiParam(name = "password", value = "密码") @RequestParam(value = "password",required = false) String password,
                                               @ApiParam(name = "username", value = "用户名") @RequestParam(value = "username",required = false) String username,
                                               @ApiParam(name = "age", value = "年龄") @RequestParam(value = "age",required = false) Integer age,
                                               @ApiParam(name = "address", value = "地址") @RequestParam(value = "address",required = false) String address,
                                               @ApiParam(name = "phone", value = "手机号码") @RequestParam(value = "phone",required = false) Integer phone,
                                               @ApiParam(name = "gender", value = "性别") @RequestParam(value = "gender") Character gender,
                                               @ApiParam(name = "birth", value = "出生日期") @RequestParam(value = "birth",required = false) String birth
    ) {
        UserEntity userEntity = new UserEntity();
        userEntity.setId(id);
        userEntity.setGender(gender);
//        userEntity.setPassword(passwordEncoder.encode(password));
        userEntity.setPassword(password);
        userEntity.setUpdateTime(LocalDateTime.now());
        userEntity.setPhoneNumber(phone);
        userEntity.setUserName(username);
        if (!StringUtils.isEmpty(address)) {
            userEntity.setAddress(address);
        }
        if (!StringUtils.isEmpty(age)) {
            userEntity.setAge(age);
        }
        if (!StringUtils.isEmpty(birth)) {
            userEntity.setBirth(LocalDateTime.parse(birth, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        return userService.updateById(userEntity);
    }
}
